package com.example.sp03.controller;

import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.UserService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2022/1/14 15:42
 */
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/{userId}")
    public JsonResult getUserById(@PathVariable("userId") Integer userId) {
        log.info("获取用户请求：{}",userId);
        return JsonResult.build().code(JsonResult.SUCCESS).data(userService.getUser(userId));
    }

    @GetMapping("/{userId}/score")
    public JsonResult addUserScore(@PathVariable("userId")Integer userId,Integer score){
        log.info("为用户{}增加积分的请求：{}",userId,score);
        userService.addScore(userId, score);
        return JsonResult.build().code(JsonResult.SUCCESS).msg(String.format("用户 %s 增加了 %s 积分",userId , score));
    }
    @GetMapping("/list")
    public JsonResult<List<User>> getUsers(){
        return JsonResult.build().code(JsonResult.SUCCESS).msg("调用成功").data(userService.getUserList());
    }

    @GetMapping("/favicon.ico")
    public void ico(){}
}
